package com.hd.boot0407.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.captcha.generator.MathGenerator;
import com.hd.boot0407.util.Constants;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/hutool")
public class HutoolCaptcharServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.创建一个验证码对象
        LineCaptcha captcha = CaptchaUtil.createLineCaptcha( 200, 100);
        System.out.println(captcha);
        captcha.setGenerator(new MathGenerator());
       /* final String code = captcha . getCode();
        System.out.println(code);*/
       /* req.getSession().setAttribute("captcha",captcha);*/
        // 把captcha对象放入session中
        HttpSession session = req.getSession();
        session.setAttribute(Constants.HUTOOL_SESSION_KEY,captcha);
       /* final boolean verify = captcha.verify(code);
        System.out.println(verify) ;*/
        //2．把验证码对象返回给浏览器
        captcha.write(resp.getOutputStream());


    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
